var ptype = ''; 
var w=null;
var url = '';
var pays = {};
var spid = nums = did = shul = member_id = ws = '';
var money =  zf_money = 0;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	member_id = plus.storage.getItem('id');
	
	 
// 获取支付通道
	plus.payment.getChannels(function(channels){
//		console.log(JSON.stringify(channels));
		for(var i in channels){
			var channel=channels[i];
			pays[channel.id]=channel;
			checkServices(channel);
		}
	},function(e){ 
		outLine("获取支付通道失败："+e.message);
	});

},false)  
	      
//支付
function payment(price){
//	console.log(ordernum+" "+price+" "+ptype);return;
	//预设订单Id ——> 用户Id+当前时间戳
	orderid = new Date().getTime(); 
	if(w){return;}//检查是否请求订单中
	if(ptype == 'alipay'){ 
//		console.log(webRoot + '/mobile/api/pay/Alipay.php?orderid=' + orderid + '&price='+price);
		url = webRoot + '/mobile/api/pay/Alipay.php?orderid=' + orderid + '&price='+price;
	}else if(ptype == 'wxpay'){
//		console.log(webRoot + '/pay/index.php?orderid=' + orderid + '&price='+price);
		url = webRoot + '/mobile/api/pay/index.php?orderid=' + orderid + '&price='+price;
	}
	w = plus.nativeUI.showWaiting('支付中...',{width:'80px',height:'80px',background:'rgba(0,0,0,0.3)'});
	// 请求支付订单
	var xhr=new XMLHttpRequest();
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = function() {
		switch (xhr.readyState) {
			case 4:
			w.close();  
			w = null;  
			if (xhr.status == 200) { 
				var order = xhr.responseText;
				console.log(order);  
				console.log(JSON.stringify(pays[ptype])); 
				plus.payment.request(pays[ptype], order, function(result) { 
					//对订单进行操作
					console.log(JSON.stringify(result));
					campus_goods(remuneration);
				}, function(e) {
					toast("支付失败,订单已生成！");
					$('.popover').fadeOut();
					$('.btn').slideUp()
					ws.close();
//					console.log(JSON.stringify(e));
					console.log("["+e.code+"]："+e.message);
				});
			} else { 
				plus.nativeUI.toast("获取订单信息失败！", null, "捐赠");
			}
			break;
		default:
			break;
		}
	}
	xhr.open('GET', url);
	xhr.send();
}

////余额支付
function yuezhifu(paypass){
//	console.log(member_id +"==="+order_id);
	$.ajax({ 
		type:"post", 
		url:apiRoot + "?act=member_buy&op=pay_state_campus",
		data:{
			member_id:member_id,
			paypass:paypass,
			remuneration:remuneration
		},  
		dataType:'json', 
		success:function(data){
//			console.log(JSON.stringify(data));
			if(data.code == 200){
//				toast('支付成功！');
				//隐藏支付密码框
				$("#j_password_of_pay").hide();
				tool.shade().hide();
				$('.popover').fadeOut();
				campus_goods();
				
			}else{
				if(data.datas.error == '余额不足'){
					toast(data.datas.error);
					//隐藏支付密码框
					$("#j_password_of_pay").hide();
					tool.shade().hide();
					$('.popover').fadeOut();
					$('.mui-clearfix input').val('');
					$('#password_backdrop').css("display","none");
				}else{
					toast(data.datas.error);
					$('.mui-clearfix input').val('');
				}
				 
			} 
		},
		error:function(e){
			error();
		}
	});
}
//	
//	
//	
//	
//	
//	

// 检测是否安装支付服务
function checkServices(pc){
	if(!pc.serviceReady){
		var txt=null;
		switch(pc.id){
			case "alipay":
			txt="检测到系统未安装“支付宝快捷支付服务”，无法完成支付操作，是否立即安装？";
			break;
			default:
			txt="系统未安装“"+pc.description+"”服务，无法完成支付，是否立即安装？";
			break;
		} 
		plus.nativeUI.confirm(txt,function(e){
			if(e.index==0){
				pc.installService();
			}
		},pc.description);
	}
}


function campus_goods(){
	$.ajax({
		type:"post",
		url:apiRoot + "?act=member_campus&op=addcampus",
		data:{
			member_id:member_id,
			title:title,
			store_id:store_id,
			targettime:targettime,
			remuneration:remuneration,
			place:place,
			address:address,
			contacts:contacts,
			contacts_tel:contacts_tel,
			demand:demand,
			pics:pictures
		},
		dataType:'json',
		success:function(data){
			console.log(JSON.stringify(data));
			if(data.code == 200){
				toast('发布成功！');
				plus.webview.getWebviewById(spage).reload();
				setTimeout(function(){
					ws.close();
				},500)
			}else{
				toast(data.datas.error);
			}
		},
		error:function(e){
			console.log(JSON.stringify(e));
			error();
		}
	});
}
